<?php
/**
* 
*/
class AdminUserModel extends MY_Model
{
	
	function __construct()
	{
		parent::__construct();
		
		$this->table = 'nmp_admin';
	}


	//获取列表数据
	public function get_admin_data()
	{
		$this->db->select("id, name, headimg, addtime, last_time, remarks ");
		$this->db->from($this->table);

		$query = $this->db->get();

		$data = $query->result_array();

		foreach ($data as $key => $value) 
		{
			$data[$key]['addtime'] = $value['addtime'] ? date('Y-m-d H:i:s',$value['addtime']) : 'N/A';
			$data[$key]['last_time'] = $value['last_time'] ? date('Y-m-d H:i:s',$value['last_time']) : 'N/A';
		}

		return $data;

	}


	/* 获取管理员信息
	 * $admin_id   管理员id
	*/
	public function admin_data_row($admin_id='')
	{
		$this->db->select("id, name, remarks, headimg");
		$this->db->from($this->table);
		$this->db->where('id', $admin_id);

		$query = $this->db->get();

		return $query->row_array();
	}


	/* 添加管理员
	 * $arr			账户信息数组
	 * name     用户名
	 * password 	密码
	 * add_time     添加时间
	*/
	public function insert_data($arr = array())
	{
		//检查用户名是否重复
		$arr['name'] = trim($arr['name']);
		$arr['password'] = md5($arr['password']);
		$arr['addtime'] = time();

		$row = $this->getRow(array('name'=>$arr['name']));

		if ($row) 
		{
			$res['name'] = $row['name'];
			return $res;
		}

		//取出角色的action_list
		// $query = $this->db->query("SELECT action_list FROM sl_role where role_id = '{$arr['role_id']}' LIMIT 1;");
		// $action_list = $query->row();

		// $arr['action_list'] = $action_list->action_list;

		//添加入库
		if ($this->db->insert($this->table,$arr)) 
		{
			$res['admin_id'] = $this->db->insert_id();

			//记录系统日志
			// $this->admin_log(array('opera_type'=>'添加', 'opera_desc'=>'添加用户'.$arr['username'], 'opera_modu'=>'添加用户'));

			return $res;
		}
		else
		{
			return false;
		}

	}


	/* 更新管理员信息
	 * $arr			账户信息数组
	 * username     用户名
	 * password 	密码
	 * admin_id		主键id
	*/
	public function update_user_data($arr = array(),$admin_id ='')
	{
		$apdate_arr = array();

		$row = $this->getRow(array('name'=>$arr['name']));

		//检查更新得用户名是否已存在
		if ($row) 
		{
			if ($row['id'] != $admin_id){
				$res['name'] = $row['name'];
				return $res;
			}
		}

		//如果密码为空则不更改
		if (!empty($arr['password'])) {
			$apdate_arr['password'] = md5($arr['password']);
		}

		// if ($admin_id > 1) {
		// 	//取出角色的action_list
		// 	$query = $this->db->query("SELECT action_list FROM sl_role where role_id = '{$arr['role_id']}' LIMIT 1;");
		// 	$action_list = $query->row();

		// 	$apdate_arr['action_list'] = $action_list->action_list;
		// 	$apdate_arr['role_id'] = $arr['role_id'];

		// }else{

		// 	$apdate_arr['action_list'] = 'all';
		// 	$apdate_arr['role_id'] = 0;
		// }


		$apdate_arr['name'] = $arr['name'];
		$apdate_arr['remarks'] = $arr['remarks'];
		$apdate_arr['headimg'] = $arr['headimg'];


		if ($this->db->update($this->table,$apdate_arr,array('id'=>$admin_id))){			
			return true;
		}else{
			return false;
		}

	}



	/* 删除
	 * $aid   主键id
	*/
	public function delete_admin_user($aid)
	{
		return $this->db->delete($this->table, array('id'=>$aid));
	}


}





?>